package com.ftg.learn.util;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Guo
 * @date 2022/3/13
 */
public class ExportExcelUtil {

    public static void exportXls(List<Map<String, Object>> list,
                                 LinkedHashMap<String, Object> map,
                                 String excelName, HttpServletResponse response) {

        //读取excel模板
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(excelName);
        // 设置列宽
        for (int i = 0; i < map.size(); i++) {
            sheet.setColumnWidth((short) i, (short) 6000);
        }
        // 设置表头样式
        XSSFCellStyle cellstyle = wb.createCellStyle();
        // 设置居中
        cellstyle.setAlignment(HorizontalAlignment.CENTER);
//        cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 创建标题样式
        XSSFCellStyle headerStyle = wb.createCellStyle();
        //设置垂直居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置水平居中
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        //创建字体样式
        XSSFFont headerFont = wb.createFont();
        // 字体加粗
        headerFont.setBold(true);
        //设置字体类型
        headerFont.setFontName("Times New Roman");
        //设置字体大小
        headerFont.setFontHeightInPoints((short) 14);
        //为标题样式设置字体样式
        headerStyle.setFont(headerFont);
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell((short) 0);
        int sum = 1;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            cell.setCellValue(entry.getValue().toString());
            cell.setCellStyle(headerStyle);
            cell = row.createCell((short) sum++);
        }
        for (int i1 = 0; i1 < list.size(); i1++) {
            row = sheet.createRow(i1 + 1);
            Map<String, Object> ListMap = list.get(i1);
            int sum1 = 0;
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                row.createCell(sum1++).setCellValue(ListMap.get(entry.getKey()) == null ? "" : String.valueOf(ListMap.get(entry.getKey())));
            }
        }
        OutputStream output = null;
        try {
            output = response.getOutputStream();
            response.reset();
            String str = excelName + ".xlsx";
            response.setHeader("Content-disposition", "attachment;filename=" + new String(str.getBytes("gbk"), "iso8859-1"));
            wb.write(output);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
